//
//  LcAlertView.m
//  iphone_lczn
//
//  Created by sunshine on 12-3-5.
//  Copyright (c) 2012年 sun. All rights reserved.
//

#import "WyAlertView.h"
#import <QuartzCore/QuartzCore.h>

@implementation WyAlertView
@synthesize isMiss;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        isMiss=YES;
        lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        lable.backgroundColor=[UIColor clearColor];
        lable.textColor=[UIColor whiteColor];
        lable.textAlignment=NSTextAlignmentCenter;
        lable.numberOfLines=10;
        [self addSubview:lable];
        
        self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.6];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 4.0;
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = [[UIColor grayColor] CGColor];
        _orgainRect=frame;
        
        image = [[UIImageView alloc]init];
        [self addSubview:image];
        // Initialization code
    }
    return self;
}

-(void)show:(NSString*)msg{
    [self show:msg state:StMsgStateWrong];
}

-(void)show:(NSString*)msg state:(StMsgState)state{
    NSString * tempmsg=@"\n\n";
    tempmsg =[tempmsg stringByAppendingString:msg];
    
    
    CGSize rectsize = CGSizeMake(_orgainRect.size.width,960); //设置一个行高上限
    NSDictionary *attribute = @{NSFontAttributeName: lable.font};
    CGSize size = [msg boundingRectWithSize:rectsize options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(Dismiss:) userInfo:nil repeats:NO];
    CGFloat y=_orgainRect.origin.y+(_orgainRect.size.height-size.height-20)/2.0;
    self.frame=CGRectMake(self.frame.origin.x, y, self.frame.size.width, size.height+25);
    lable.text=msg;
    image.frame = CGRectZero;
    if (state == StMsgStateRight){
        image.image = [UIImage imageNamed:@"msg_right"];
        image.frame = CGRectMake((self.frame.size.width - 30) / 2, 15, 30, 30);
    }
    if (state == StMsgStateWrong){
        image.image = [UIImage imageNamed:@"msg_wrong"];
        image.frame = CGRectMake((self.frame.size.width - 30) / 2, 15, 30, 30);
    }
    lable.frame=CGRectMake(0, CGRectGetMaxY(image.frame) + 5, self.frame.size.width, self.frame.size.height-10);
    self.frame=CGRectMake(self.frame.origin.x, y, self.frame.size.width, size.height+20+CGRectGetMaxY(image.frame) + 5);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}
-(void)Dismiss:(NSTimer *)timer{
    //NSLog(@"%s",__FUNCTION__);
    [self removeFromSuperview];
}
-(void)setIsMiss:(BOOL)_isMiss{
    isMiss=_isMiss;
}
//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
//    [self removeFromSuperview];
//    return YES;
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
